import QQMap from '../../../utils/qqmap'
// map.js
Page({
  data: {
    latitude: 26.743703,
    longitude: 110.629117,
    markers: [
      {
        id: 1,
        latitude: 28.23529,
        longitude: 112.93134,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 26.743703,
        longitude: 110.629117,
        iconPath: '../../../static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
    repairDetail: {},
  },
  onLoad({ id }) {
    this.getRepairDetail(id)
    this.getPolyline()
  },
  async getRepairDetail(id) {
    if (!id) return wx.utils.toast('参数有误!')
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast()
    console.log({ repairDetail });
    this.setData({ repairDetail })
  },
  // 取消报修
  async cancelRepair(ev) {
    // console.log(ev);
    const { code } = await wx.http.put('/cancel/repaire/' + ev.mark.id)
    if (code !== 10000) wx.utils.toast()
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })

  },
  getPolyline() {
    QQMap.direction({
      mode: 'bicycling',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: '28.23529,112.93134',
      to: '26.743703,110.629117',
      success: (res) => {
        console.log(res);
        let ret = res;
        let coors = ret.result.routes[0].polyline,
          pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        this.setData({
          latitude: pl[30].latitude,
          longitude: pl[30].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  }
})
